package cn.lichenes.controller;

import cn.lichenes.entity.Address;
import cn.lichenes.entity.Indent;
import cn.lichenes.entity.User;
import cn.lichenes.log.SystemLog;
import cn.lichenes.service.AddressService;
import cn.lichenes.service.IndentService;
import cn.lichenes.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UsersController {

    @Autowired
    private UserService userService;
    @Autowired
    private IndentService indentService;
    @Autowired
    private AddressService addressService;

    @GetMapping("/management-users")
    public String managementUsers(@RequestParam(value = "page",defaultValue = "1")Integer page,@RequestParam(value = "username",required = false) String username, Model model){
        Page<User> userPage = userService.findAllUser(username,page);
        List<User> users = userPage.getContent();
        Map<String,Object> map = new HashMap<>();
        map.put("users",users);
        map.put("currentPage",page);
        map.put("totalPages",userPage.getTotalPages());
        map.put("username",username);
        model.addAllAttributes(map);
        return "management-users";
    }

    @SystemLog(value = "修改用户状态",status = "admin")
    @PutMapping("/management-users/{id}")
    public String banUser(@PathVariable(value = "id")Integer id, Integer status){
        boolean flag = userService.banUser(id,status);
        System.out.println(flag);
        return "redirect:/management-users";
    }

    @GetMapping("/account")
    public String account(HttpSession session,Model model){
        User user = (User) session.getAttribute("user");
        if (user != null){
            Page<Indent> indentPage = indentService.findByUser(user, 1);
            List<Address> addresses = addressService.findAll(user);
            Map<String,Object> map = new HashMap<>();
            map.put("addresses",addresses);
            map.put("indents",indentPage.getContent());
            map.put("currentPage",1);
            map.put("totalPages",indentPage.getTotalPages());
            model.addAllAttributes(map);
            return "account";
        }
        return "redirect:/login";
    }

    @SystemLog(value = "用户修改信息",status = "user")
    @PutMapping("/account/{id}")
    public String updateaccount(@PathVariable("id") Integer id,
                                String name,String phone,String email){
        boolean flag = userService.updateUser(id, name, phone, email);
        return "redirect:/account";
    }
}
